
const { ccclass, property } = cc._decorator;
let winSize = cc.winSize;
let GROUPINDEX = 5.5;
@ccclass
export default class Screenshots extends cc.Component {


    @property(cc.Sprite)
    see: cc.Sprite = null;


    // _camera:cc.Camera = null;
    // LIFE-CYCLE CALLBACKS:
    _texture: cc.RenderTexture = null;

    //传入保存的目标结点   截图后保存到包中
    saveImg(targetNode: cc.Node, needDisable) {
        if (!targetNode) {
            targetNode = this.node;
        }
        //节点未被激活
        if (!targetNode.activeInHierarchy) {
            return;
        }
        //新建一个 RenderTexture，
        //并且设置 camera 的 targetTexture 为新建的 RenderTexture，
        //这样 camera 的内容将会渲染到新建的 RenderTexture 中。
        let boundingBox = targetNode.getBoundingBox();
        if (!boundingBox.width || !boundingBox.height) {
            //没有面积,不做截取
            cc.warn("没有面积")
            return;
        }

        //暂存groupIndex  用完后恢复
        let oldGI = targetNode.groupIndex;
        //给一个不同的值 防止透明区域截到其他组节点的数据
        targetNode.groupIndex = GROUPINDEX;//小数不会和其他组冲突

        let camera = targetNode.addComponent(cc.Camera);
        //摄像机显示的内容
        camera.cullingMask = targetNode._cullingMask; //Mark:此处可能和其他节点会有冲突,如果使用了分组,请将GROUPINDEX值设置大些;
        let scale = winSize.height / boundingBox.height
        camera.zoomRatio = scale;
        let texture = new cc.RenderTexture();


        if (this.see) {
            let spriteFrame = new cc.SpriteFrame();
            spriteFrame.setTexture(texture)
            this.see.spriteFrame = spriteFrame;

        }
        let t_width = boundingBox.width;
        let t_height = boundingBox.height



        let gl = cc.game._renderContext;
        texture.initWithSize(t_width, t_height, gl.STENCIL_INDEX8);
        camera.targetTexture = texture;
        this._texture = texture;
        if (needDisable) {
            targetNode.active = true;
        }



        // 渲染一次摄像机，即更新一次内容到 RenderTexture 中
        camera.render(undefined);


        if (needDisable) {
            targetNode.active = false;
        }
        // 这样我们就能从 RenderTexture 中获取到数据了
        let data = texture.readPixels();


        let width = texture.width;
        let height = texture.height;
        let picData = this._flipYImage(data, width, height);

        // cc.log("picData = ", picData);
        let path = this._saveFile(picData)

        // camera.render(undefined);
        targetNode.removeComponent(cc.Camera)
        this._texture = null;
        targetNode.groupIndex = oldGI;
        return path;
    }

    //反转
    _flipYImage(data, width, height) {
        // create the data array
        let picData = new Uint8Array(width * height * 4);
        let rowBytes = width * 4;
        for (let row = 0; row < height; row++) {
            let srow = height - 1 - row;
            let start = srow * width * 4;
            let reStart = row * width * 4;
            // save the piexls data
            for (let i = 0; i < rowBytes; i++) {
                picData[reStart + i] = data[start + i];
            }
        }
        return picData;
    }

    _saveFile(picData) {
        if (CC_JSB) {
            let filePath = jsb.fileUtils.getWritablePath() + 'result_share.png';
            if (jsb.fileUtils.isFileExist(filePath)) {
                jsb.fileUtils.removeFile(filePath);
            }
            let success = jsb.saveImageData(picData, this._texture.width, this._texture.height, filePath)
            if (success) {
                cc.vv.GLSDKMgr.saveImageToPhotos(filePath);
            } else {
                cc.vv.GLUtils.showTips("保存失败!")
                return null;
            }
            return filePath;
        }
    }
}
